package com.aaa.system.service.impl;

import com.aaa.common.entity.DictData;
import com.aaa.common.vo.Result;
import com.aaa.system.dao.DictDataDao;
import com.aaa.system.service.DictDataService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @program: aaa-hospital-parent
 * @description:
 * @author: 闫克起
 * @create: 2024-05-22 14:45
 **/
@Service
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private DictDataDao dictDataDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<List<DictData>> getType(String type){
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        try {
            //从缓存中获取数据字典的内容//从缓存中获取数据字典的内容
            String s = forValue.get("dict::" + type);
            if (StringUtils.hasText(s)) {
                List<DictData> dictData = JSON.parseArray(s, DictData.class);
                return new Result<>(2000, "查询状态成功", dictData);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        List<DictData> dictData = dictDataDao.selectList(wrapper);
        //放入缓存中
        forValue.set("dict::"+type,JSON.toJSONString(dictData));
        return new Result<>(2000,"查询状态成功",dictData);
    }
}
